<?php

/**
 * 搜索索引
 */
namespace app\topics\model;

use app\system\model\SystemModel;
use dux\lib\Http;

class TopicsSearchModel extends SystemModel {

    protected $infoModel = [
        'pri' => 'id',
        'validate' => [
        ],
        'format' => [
        ],
        'into' => '',
        'out' => '',
    ];

    public function getKeywords($title, $content, $encode = 'utf-8') {
        if($title == ''){
            return [];
        }
        return [];
        $title = rawurlencode(strip_tags($title));
        $content = strip_tags($content);
        if(strlen($content)>2400){
            $content =  mb_substr($content, 0, 800, $encode);
        }
        $content = rawurlencode($content);
        $url = 'http://keyword.discuz.com/related_kw.html?title='.$title.'&content='.$content.'&ics='.$encode.'&ocs='.$encode;
        try {
            $xml_array=simplexml_load_file($url);
        }catch (\Exception $e) {
            return [];
        }
        $result = $xml_array->keyword->result;
        $data = array();
        foreach ($result->item as $key => $value) {
            array_push($data, (string)$value->kw);
        }
        if(count($data) > 0){
            return $data;
        }else{
            return [];
        }
    }

    public function indexData($detailId, $title, $content, $tag) {
        $content = html_clear($title . ' ' . $content . ' ' . $tag);
        $content = \dux\lib\Str::symbolClear($content);


 /*       $data = \dux\lib\Http::curlPost('http://www.xunsearch.com/scws/api.php', [
            'data' => $content,
            'respond' => 'json',
            'ignore' => true,
            'multi' => 3
        ],100);

        browser_log($data);
        if(empty($data)) {
            return true;
        }

        browser_log($data);
        $data = json_decode($data, true);

        if($data['status'] <> 'ok') {
            return true;
        }

        $words = $data['words'];*/

        $words = $this->splitKeywords($content);
        $words = array_filter($words);

        $str = implode(' ', $words);
        $str = str_replace("\n", ' ', $str);
        $str = trim($str);

        $pingyin = new \dux\lib\Pinyin();
        $str = $pingyin->output($str);

        $info = $this->getWhereInfo([
            'detail_id' => $detailId
        ]);
        $data = [];
        $data['detail_id'] = $detailId;
        $data['search'] = $str;
        if ($info) {
            $data['id'] = $info['id'];
            $this->edit($data);
        } else {

            $this->add($data);
        }
        return true;

    }

    public function strEncode($str) {
        $src = array("/", "+", "=", " ");
        $dist = array("_a", "_b", "_c", "_d");
        $str = strtolower($str);
        $str = base64_encode($str);
        $str = trim($str);
        return str_replace($src, $dist, $str);

    }

    public function splitKeywords($content) {
        \dux\Dux::import('app/topics/util/pscws/pscws4');
        $pscws = new \PSCWS4('utf8');
        $pscws->set_charset('utf8');
        $pscws->set_dict(ROOT_PATH . 'app/topics/util/pscws/etc/dict.utf8.xdb');
        $pscws->set_rule(ROOT_PATH . 'app/topics/util/pscws/etc/rules.utf8.ini');
        $pscws->set_ignore(true);
        $pscws->set_multi(2);
        $pscws->set_duality(true);
        $pscws->send_text($content);
        $words = [];
        while ($some = $pscws->get_result()) {
            foreach ($some as $word) {
                $words[] = $word['word'];
            }
        }
        $pscws->close();
        return $words;
    }

}